package com.example.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
@PropertySource({"classpath:/configinject/config.properties",
        "classpath:/configinject/config_${anotherfile.configinject}.properties"})
public class ConfigurationFileInject {

    @Value("${book.name}")
    private String bookName;

    @Value("${book.name.placeholder}")
    private String bookNamePlaceholder;

    @Autowired
    private Environment env;  // 注入环境变量对象，存储注入的属性值

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("bookName=").append(bookName).append("\r\n")
                .append("bookNamePlaceholder=").append(bookNamePlaceholder).append("\r\n")
                .append("env=").append(env).append("\r\n")
                .append("env=").append(env.getProperty("book.name.placeholder")).append("\r\n");    // 从eniroment中获取属性值
        return sb.toString();
    }
}
